package gal;

import gal.obj.Body2D;
import gal.obj.Star2D;
import gal.util.Gutil;

import java.util.ArrayList;

import javax.swing.JFrame;

import swg.utils.RunningTime;

public class Generator
{
	/*
	 * units =  lenght:1 light/year = 394189008627834 m
	 * 			mass: 1 solar mass = 1,9891e30 kg
	 * 			time: 1yr = 1314873 s
	 * */

	
		
	
	
	public static void main(String[] args)
	{
		Star2D s = new Star2D(1, 2, 3, 4, 5);
		
		double dt=Gutil.m;
		ArrayList<String> path= new ArrayList<String>();
		ArrayList<Body2D[]> timeLine= new ArrayList<Body2D[]>();
		Body2D sun = new Star2D(0, 0, 0, 0, 1.9891e30);
		Body2D earth = new Star2D(152097701000d, 0, 0, 29291, 5.9742e24);	
//		Body2D venus = new Star2D(-108941849000d, 0, 0, -34784, 4.8685e24);	
//		Body2D mercury = new Star2D(69817079000d, 0, 0, 38860, 3.302e23);	
//		Body2D mars = new Star2D(-249228730000d, 0, 0, -21972, 6.4185e23);	
//		Body2D jupiter = new Star2D(816081455000d, 0, 0, 12446, 1.8986e27);	
		
//		Body2D[] universe = new Body2D[]{sun,earth,venus, mercury,mars,jupiter};
		Body2D[] universe = new Body2D[]{sun,earth};
		int i=0;
		int time = (int) (Gutil.y*12000/dt);
		int samples= 20000;
		int sampling = time/samples;
		RunningTime rt = new RunningTime();
		rt.start();
		for( i=0;i<time;i++){
			if (i%sampling==0){
				int  p= (int) (((i*1.0)/(time*1.0))*100d);
				timeLine.add(new Body2D[]{sun.clone(),earth.clone()});
//				timeLine.add(new Body2D[]{sun.clone(),earth.clone(), venus.clone(), mercury.clone(),mars.clone(),jupiter.clone()});
				System.out.println(i+"/"+ time + "="+ p+"%");
			}
			universe=move(universe, dt);
			//System.out.println(s2.getvX()+"\t,\t"+s2.getvY());
			//path.add(s2.getX()+","+s2.getY()+";");
		}
		rt.stop();
		System.out.println(rt);
		System.out.println(timeLine.size());

		JFrame frame = new JFrame("Orbit");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.add(new Points(timeLine));
		frame.setSize(800, 800);
		frame.setLocationRelativeTo(null);
		frame.setVisible(true);
			
	}

	private static Body2D[] move(Body2D[] uni, double dt)
	{
		for(int i =1;i<uni.length;i++){
			double fx=0;
			double fy=0;
			for(int j=0;j<uni.length;j++){
				if (i==j) continue;
				double distance = Gutil.distance(uni[i], uni[j]);

				double f=Gutil.G*(uni[j].getMass()*uni[i].getMass())/(distance*distance);
				fx=fx+f*(uni[j].getX()-uni[i].getX())/distance;
				fy=fy+f*(uni[j].getY()-uni[i].getY())/distance;
			}
			uni[i].setX(uni[i].getX()+uni[i].getvX()*dt+fx/uni[i].getMass()*dt*dt*.5);
			uni[i].setY(uni[i].getY()+uni[i].getvY()*dt+fy/uni[i].getMass()*dt*dt*.5);
			uni[i].setvX(uni[i].getvX()+fx/uni[i].getMass()*dt);
			uni[i].setvY(uni[i].getvY()+fy/uni[i].getMass()*dt);
		}
		
		return uni;
	}


}
